package com.acwing.partition12;

import java.io.*;
import java.util.PriorityQueue;

/**
 * @author `RKC`
 * @date 2022/1/8 10:05
 */
public class AC1129热浪dijkstra {

    private static final int N = 2510, M = 6210 * 2, INF = 0x3f3f3f3f;
    private static int[] head = new int[N], element = new int[M], next = new int[M], weight = new int[M];
    private static int index = 0, n = 0, m = 0;
    private static int[] dist = new int[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        int start = Integer.parseInt(s[2]), end = Integer.parseInt(s[3]);
        for (int i = 0; i < N; i++) {
            head[i] = -1;
            dist[i] = INF;
        }
        for (int i = 0; i < m; i++) {
            s = reader.readLine().split("\\s+");
            int u = Integer.parseInt(s[0]), v = Integer.parseInt(s[1]), w = Integer.parseInt(s[2]);
            createEdge(u, v, w);
            createEdge(v, u, w);
        }
        writer.write(dijkstra(start, end) + "\n");
        writer.flush();
    }

    private static int dijkstra(int start, int end) {
        //(当前点，距离起点的位置)
        PriorityQueue<int[]> heap = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[1], o2[1]));
        heap.add(new int[]{start, 0});
        dist[start] = 0;
        while (!heap.isEmpty()) {
            int[] curr = heap.poll();
            int u = curr[0], w = curr[1];
            if (u == end) return w;
            for (int i = head[u]; i != -1; i = next[i]) {
                int v = element[i];
                if (w + weight[i] < dist[v]) {
                    dist[v] = w + weight[i];
                    heap.add(new int[]{v, dist[v]});
                }
            }
        }
        return -1;
    }

    private static void createEdge(int u, int v, int w) {
        element[index] = v;
        next[index] = head[u];
        head[u] = index;
        weight[index++] = w;
    }
}
